﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using DAMNETLibrary.Controllers;
using DAMNETLibrary.Models;

namespace DAMNETWebApplication
{
    /// <summary>
    /// 
    /// </summary>
    public class CommonPage : Page
    {
        private Exception ex;

        /// <summary>
        /// 
        /// </summary>
        public Exception LastException
        {
            get
            {
                return ex;
            }
            set
            {
                if (value == null)
                {
                    DisplayError = "Unknown error has occured.";
                    return;
                }

                if (value.GetType() != typeof(DAMNETLibrary.DAMNETException))
                    ; //Mailer.SendException(value);

                ex = value;
                DisplayError = ex.Message;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string DisplaySuccess
        {
            set
            {
                Controls.ErrorDisplay error = (Controls.ErrorDisplay)this.BaseMasterPage.FindControl("errorDisplay");
                error.SuccessText = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string DisplayError
        {
            set
            {
                Controls.ErrorDisplay error = (Controls.ErrorDisplay)this.BaseMasterPage.FindControl("errorDisplay");
                error.ErrorText = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool HasError
        {
            get
            {
                if (LastException != null)
                    return true;

                Controls.ErrorDisplay error = (Controls.ErrorDisplay)this.BaseMasterPage.FindControl("errorDisplay");
                return error.Visible;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public MasterPage BaseMasterPage
        {
            get
            {
                MasterPage lastMaster = null;
                for (MasterPage mp = this.Master; mp != null; mp = mp.Master)
                    lastMaster = mp;

                return lastMaster;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearErrorSuccess()
        {
            Controls.ErrorDisplay error = (Controls.ErrorDisplay)this.BaseMasterPage.FindControl("errorDisplay");
            error.ClearError();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_PreLoad(object sender, EventArgs e)
        {
            ClearErrorSuccess();
            ex = null;

            if(!IsPostBack)
                Page.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!HasError)
            {
                if (!IsPostBack)
                {
                    switch (Request["errorcode"])
                    {
                        case "":
                            DisplaySuccess = "";
                            break;
                    }
                }
            }

            base.OnPreRender(e);
        }
    }
}